package demo.practice.linklist;

import demo.practice.ListNode;

public class P2 {


    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {

        ListNode cur1 = l1;
        ListNode cur2 = l2;

        ListNode head = null;
        ListNode cur = null;
        int jw = 0;
        while (cur1 != null || cur2 != null) {
            int sum = (cur1 == null ? 0 : cur1.val) + (cur2 == null ? 0 : cur2.val) + jw;
            if (cur1 != null)
                cur1 = cur1.next;
            if (cur2 != null)
                cur2 = cur2.next;

            jw = sum / 10;
            sum = sum % 10;
            if (head == null) {
                head = new ListNode(sum);
                cur = head;
            } else {
                cur.next = new ListNode(sum);
                cur = cur.next;
            }
        }

        if(jw>0)
            cur.next=new ListNode(jw);
        return head;
    }

}
